package com.example.zhangwei.widgts;

import com.example.zhangwei.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyCustomView extends View{
	
	private final static String TAG = "MyCustomView";
	
	private Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息  
	
	private Paint mPaint2; //画笔,包含了画几何图形、文本等的样式和颜色信息  
	
	private String text;

	public MyCustomView(Context context) 
	{
		super(context);
		Log.e(TAG, "--------------------------MyCustomView(Context context)");
		// TODO Auto-generated constructor stub
	}
	
	public MyCustomView(Context context, AttributeSet attrs)
	{  
		super(context, attrs);
		Log.e(TAG, "-------------------------MyCustomView(Context context, AttributeSet attrs)");
		mPaint = new Paint();
		mPaint2 = new Paint();
		//TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组  
        //在使用完成后，一定要调用recycle方法  
        //属性的名称是styleable中的名称+“_”+属性名称  
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);  
        text = array.getString(R.styleable.MyView_text); //提供默认值，放置未指定  
        int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值，放置未指定  
        float textSize = array.getDimension(R.styleable.MyView_textSize, 36);  
        mPaint2.setColor(textColor);  
        mPaint.setTextSize(textSize);  
        array.recycle(); //一定要调用，否则这次的设定会对下次的使用造成影响  
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
//		Log.e(TAG, "-------------------------onDraw");
		//Canvas中含有很多画图的接口，利用这些接口，我们可以画出我们想要的图形  
		mPaint2.setStyle(Style.FILL); //设置填充  
		canvas.drawRect(10, 10, 300, 100, mPaint2); //绘制矩形  

		mPaint.setColor(Color.BLUE);  
		canvas.drawText(text, 20, 60, mPaint);  
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		Log.e(TAG, "-------------------------onFinishInflate");
		super.onFinishInflate();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		Log.e(TAG, "-------------------------onMeasure");
		Log.e(TAG, "widthMeasureSpec:"+widthMeasureSpec+"|heightMeasureSpec:"+heightMeasureSpec);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

}
